<template>
	<view class="settle-container">
		<!-- 这是结算组件 -->
		<!-- 全选 -->
		<label class="radio" @click="changeAllState">
			<radio :checked="isFullCheck" color="#c00000"  /><text>全选</text>
		</label>
		<!-- 合计 -->
		<view class="amount-box">
			合计：<text class="amount">￥{{FullPrice}}</text>
		</view>
		<!-- 结算 -->
		<view class="btn-settle" @click="settlement">
			结算（{{checkedCount}}）
		</view>
	</view>
</template>

<script>
	import {mapState,mapGetters,mapMutations} from 'vuex'
	export default {
		name:"my-settle",
		data() {
			return {
				//倒计时的秒数
				seconds:3,
				//定时器的ID
				timer:null
			};
		},
		methods:{
			dayin(){
				console.log(checkedCount)
			},
			//全选按钮
			changeAllState(){
				// 当isFullCheck为false时,点击就会变true,所以这里要取反
				this.updateAllstate(!this.isFullCheck)
			},
			//结算按钮
			settlement(){
				if(!this.checkedCount)return uni.$showMsg('请选择要结算的商品')
				if(!this.addstr)return uni.$showMsg('请选择收货地址')
				//如果没有登录的话，就延时跳转到登录页
				if(!this.token)return this.delayNavigate()
				//结算支付功能
				this.payOrder()
			},
			//延时导航到my页面
			delayNavigate(){
				this.seconds = 3
				this.showTips(this.seconds)
				
				this.timer = setInterval(() => {
					this.seconds--
					if(this.seconds <= 0){
						clearInterval(this.timer)
						uni.switchTab({
							url:'/pages/my/my',
							//成功后执行的回调函数
							success: () => {
								this.updateRedirectInfo({
									openType:'switchTab',
									from:'/pages/cart/cart'
								})
							}
						})
						return
					}
					this.showTips(this.seconds)
				},1000)
			},
			//跳转到登录页面的倒计时消息
			showTips(n){
				uni.showToast({
					icon:'none',
					title:'请登录后进行结算！'+ n +'秒之后跳转到登录页',
					//mask增加一层透明层，防止用户点击穿透
					mask:true,
					//延时的时间ms
					duration:1500
				})
			},
			//结算支付功能
			async payOrder(){
				//1.创建订单
				//1.1组织订单的信息对象
				const orderInfo = {
					// order_price:this.FullPrice,
					//总价格，这里写死一下，面的支付不起
					 order_price:0.01,
					 //收货的地址
					consignee_addr:this.addstr,
					//商品的信息
					goods:this.cart.filter(x => x.goods_state).map(x => ({
						//这里因为需要的信息和存储的信息名字不同，所以需要map重新渲染
						goods_id:x.goods_id,
						goods_number:x.goods_count,
						goods_price:x.goods_price
					}))
				}
				//1.2发起请求创建订单 返回的值是promise对象，所以用await
				const {data:res} = await uni.$http.post('/api/public/v1/my/orders/create',orderInfo)
				console.log(res)
				if(res.meta.status !== 200 && res.meta.status !== 401)return uni.$showMsg('创建订单失败')
				//1.3得到服务器响应的订单编号
				//这里因为token的请求是失败的，没通过验证，所以返回的order_number写死
				// const orderNumber = res.message.order_number
				const orderNumber = 'zijizaodeqingqiudanhao'
				console.log(orderNumber)
				
				//2.订单的支付功能
				//2.1发起请求获得订单的支付信息
				const {data:res2} = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder',{order_number:orderNumber})
				console.log(res2)
				//2.2预付订单生成失败
				if(res2.meta.status !== 200 && res2.meta.status !== 401)return uni.$showMsg('预付订单创建失败')
				//2.3得到订单支付相关的必要参数
				//这里因为token的请求是失败的，没通过验证，所以返回的pay写死
				// const payInfo = res2.message.pay
				const payInfo = {nonceStr:"IetkN22VgkNYQr8T",package:"prepay_id=wx01124228305228b1aa856d4d868d4b0000",paySign:"8DA7BD5EA9CC52C520A6B6CD00B923F5",signType:"MD5",timeStamp:"1598935352"}
				console.log(payInfo)
				
				//3.发起微信支付
				//3.1调用api uni.requestPayment() 发起微信支付
				const [err,succ] = await uni.requestPayment(payInfo)
				console.log(err)
				this.updatewxzf(true)
				//3.2未完成支付
				if(err) return uni.$showMsg('订单未支付')
				//3.3完成支付查询结果
				const {data:res3} = await uni.$http.post('/api/public/v1/my/orders/chkOrder',{order_number:orderNumber})
				console.log(res3)
				//3.4检测到订单未支付
				if(res3.meta.status !== 200 && res3.meta.status !== 401)return uni.$showMsg('订单未支付')
				//3.5检测到订单支付完成
				uni.showToast({
					title:'订单支付完成！',
					icon:'success'
				})
			},
			...mapMutations('m_cart',['updateAllstate','updatewxzf']),
			...mapMutations('m_address',['updateRedirectInfo'])
		},
		computed:{
			//选择总数，购物车总数，总支付价格
			...mapGetters('m_cart',['checkedCount','total','FullPrice']),
			//token
			...mapState('m_address',['token']),
			//商品的信息
			...mapState('m_cart',['cart']),
			//映射收货地址
			...mapGetters('m_address',['addstr']),
			//是否全选 返回false和true
			isFullCheck(){
				return this.total === this.checkedCount
			}
		}
	}
</script>

<style lang="scss">
.settle-container{
	display: flex;
	justify-content: space-between;
	align-items: center;
	position: fixed;
	bottom: 0;
	left: 0;
	width: 100%;
	height: 50px;
	background-color: #fff;
	font-size: 14px;
	z-index: 99;
	padding: 5px;
	.radio{
		display: flex;
		align-items: center;
	}
	.amount-box{
		.amount{
			color: #f50000;
			font-weight: bold;
		}
	}
	.btn-settle{
		height: 50px;
		line-height: 50px;
		text-align: center;
		padding: 0 10px;
		color: #fff;
		background-color: #c00000;
		min-width: 100px;
	}
}
</style>
